// 微博数据验证工具
class WeiboDataValidator {
  
  // 验证数据质量
  static validateDataQuality(data) {
    if (!Array.isArray(data) || data.length === 0) {
      return { isValid: false, reason: '数据为空或格式错误' };
    }

    const validItems = data.filter(item => this.validateSingleItem(item));
    const validRatio = validItems.length / data.length;

    if (validRatio < 0.8) {
      return { 
        isValid: false, 
        reason: `数据质量不达标，有效数据比例: ${(validRatio * 100).toFixed(1)}%` 
      };
    }

    return { 
      isValid: true, 
      validItems: validItems.length,
      totalItems: data.length,
      quality: validRatio >= 0.95 ? '优秀' : validRatio >= 0.9 ? '良好' : '一般'
    };
  }

  // 验证单个数据项
  static validateSingleItem(item) {
    // 必需字段验证
    if (!item.title || !item.platform || !item.id) {
      return false;
    }

    // 标题质量验证
    if (item.title.length < 2 || 
        item.title.includes('undefined') || 
        item.title.includes('null') ||
        item.title.trim() === '') {
      return false;
    }

    // 数值字段验证
    if (item.heat && (isNaN(item.heat) || item.heat < 0 || item.heat > 100)) {
      return false;
    }

    if (item.views && (isNaN(item.views) || item.views < 0)) {
      return false;
    }

    return true;
  }

  // 数据清洗
  static cleanData(data) {
    return data
      .filter(item => this.validateSingleItem(item))
      .map(item => ({
        ...item,
        title: item.title.trim(),
        description: (item.description || item.title).trim(),
        views: Math.max(0, parseInt(item.views) || 0),
        comments: Math.max(0, parseInt(item.comments) || 0),
        heat: Math.min(100, Math.max(0, parseInt(item.heat) || 0))
      }));
  }

  // 检测是否为真实数据
  static detectRealData(data) {
    const realDataIndicators = [
      // 数据源标识
      item => item.source && item.source !== 'premium_mock',
      // 标题多样性
      item => !item.title.match(/^(热点话题|微博热点)\s*#?\d+$/),
      // 数据时效性
      item => {
        if (!item.updatedAt) return false;
        const updateTime = new Date(item.updatedAt);
        const now = new Date();
        return (now - updateTime) < 24 * 60 * 60 * 1000; // 24小时内
      }
    ];

    const realDataCount = data.filter(item => 
      realDataIndicators.some(indicator => indicator(item))
    ).length;

    return {
      isRealData: realDataCount > data.length * 0.3, // 30%以上认为是真实数据
      realDataRatio: realDataCount / data.length,
      totalItems: data.length,
      realItems: realDataCount
    };
  }

  // 生成数据报告
  static generateReport(data) {
    const qualityResult = this.validateDataQuality(data);
    const realDataResult = this.detectRealData(data);
    const cleanedData = this.cleanData(data);

    return {
      timestamp: new Date().toISOString(),
      summary: {
        totalItems: data.length,
        validItems: cleanedData.length,
        dataQuality: qualityResult.quality || '差',
        isRealData: realDataResult.isRealData,
        realDataRatio: `${(realDataResult.realDataRatio * 100).toFixed(1)}%`
      },
      details: {
        quality: qualityResult,
        realData: realDataResult
      },
      cleanedData: cleanedData
    };
  }
}

module.exports = WeiboDataValidator;